package q724_pivotIndex;

public class Solution_1 {
    /*
    分别求出前缀和以及后缀和来判断
    空间复杂度过高
    应使用Solution2
     */
    public int pivotIndex(int[] nums) {
        if (nums.length == 1) return 0;
        int[] cloneNums = nums.clone();
        for (int i = 1; i < nums.length; i++) {
            nums[i] += nums[i - 1];
            cloneNums[nums.length - i - 1] += cloneNums[nums.length - i];
        }
        int prev = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            if (prev == cloneNums[i + 1]) return i;
            prev = nums[i];
        }
        if (nums.length >= 2 && nums[nums.length - 2] == 0) return nums.length - 1;
        return -1;
    }
}
